メインコンテンツまでスキップ

iOS SDK

ネイティブの iOS アプリに AR Clip VPS を追加します。SDK は ARKit のトラッキングを使って、VPS マップ上のユーザーの位置を特定します。XCFramework として提供されていて、UIKit、RealityKit、SwiftUI のアプリで使えます。

始める前に​

  • iOS 15.0 以降
  • Xcode 14 以降(Swift 5.7 以降)
  • ARKit を実行できるデバイス。シミュレーターには対応していません。

インストール​

次の 2 つの方法から選びます。

Swift Package Manager​

  1. Xcode で File → Add Packages… を選びます。
  2. https://github.com/WebAR-Studio/was-vps-ios.git を入力します。
  3. 必要なブランチかタグを選び、パッケージをターゲットに追加します。

手動​

  1. リポジトリをクローンします。
    git clone https://github.com/WebAR-Studio/was-vps-ios.git
  2. WASVPS.xcframework をプロジェクトにドラッグします。
  3. General → Frameworks, Libraries, and Embedded Content で、フレームワークが埋め込まれていることを確認します。

権限を追加する​

iOS がユーザーにアクセスの許可を求められるように、カメラと位置情報の利用目的を Info.plist に追加します。

<key>NSCameraUsageDescription</key>
<string>This app requires access to the camera to display augmented reality content.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access to your location to place AR content accurately.</string>
<key>NSLocationTemporaryUsageDescriptionDictionary</key>
<dict>
<key>VPSLocalization</key>
<string>Enable precise location for accurate AR content positioning.</string>
</dict>

VPS を開始する​

VPSBuilder.initializeVPS を呼び出して VPSService を作成します。ARSession、API キー、ロケーション ID を渡します。

API キーが必要ですか?

arclip.design で取得するか、support@arclip.design までメールでお問い合わせください。

import ARKit
import WASVPS

final class VPSDemoController: UIViewController, ARSCNViewDelegate {
@IBOutlet private weak var sceneView: ARSCNView!
private var configuration: ARWorldTrackingConfiguration?
private var vps: VPSService?

override func viewDidLoad() {
super.viewDidLoad()
sceneView.delegate = self
sceneView.scene = SCNScene()

configuration = VPSBuilder.getDefaultConfiguration()

VPSBuilder.initializeVPS(
arSession: sceneView.session,
apiKey: "your-api-key",
locationIds: ["your-location-id"],
url: "https://api.arclip.app/vps/api/v3",
gpsUsage: false,
delegate: self
) { service in
self.vps = service
self.vps?.start()
}
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let configuration { sceneView.session.run(configuration) }
}

func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
vps?.frameUpdated()
}
}

extension VPSDemoController: VPSServiceDelegate {
func positionVPS(pos: ResponseVPSPhoto) {
print("Localized pose:", pos)
}

func error(err: NSError) {
print("VPS error:", err)
}

func sending(requestData: UploadVPSPhoto?) {
print("Sending localization request…")
}
}

sessionWasInterrupted でサービスを一時停止し、sessionInterruptionEnded で再び開始します。

RealityKit​

RealityKit では、ARSessionDelegate でフレームを渡します。

func session(_ session: ARSession, didUpdate frame: ARFrame) {
vps?.frameUpdated()
}

ライフサイクルの呼び出し(start、stop、clearCustomLocPos)は SceneKit と同じです。

SwiftUI​

ARSCNView か ARView を、UIViewRepresentable か UIViewControllerRepresentable で包みます。VPSService は共有のビューモデルに持たせ、SwiftUI のコントロールから start() / stop() を呼び出します。

その他のオプション​

  • 最初のローカライズの前に、わかっている変換(トランスフォーム)を setCustomLocPosForFirstRequest で渡せます。
  • 別のエンドポイントを使うには、initializeVPS に別の url を渡します。
  • gpsUsage をオンにすると、リクエストにデバイスの座標が含まれます。

トラブルシューティング​

  • VPSBuilder.getDefaultConfiguration() が nil を返す場合、そのデバイスには対応していません。
  • コールバックが一度も呼ばれない場合は、frameUpdated() が毎フレーム実行されているか、API キーとロケーション ID が有効かを確認してください。
  • UIKit、RealityKit、SwiftUI の例は /examples フォルダーにあります。